<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/20
 * Time: 14:38
 */
namespace App\Http\Controllers\Photo;

use App\Http\Controllers\WebController;
use App\Model\PhotoModel;
use App\Model\TypeModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Log;

class PhotoController extends WebController
{
    public function index(Request $request)
    {
        $searchField = $request->only(['type']);
        $data['type'] = TypeModel::get();
        $searchField = array_filter($searchField, 'strlen');
        $data['list'] = PhotoModel::orderBy('add_time', 'desc')->where($searchField)->paginate(32);
        return view('photo.index', $data);
    }

    public function create()
    {
        $data['list'] = TypeModel::get();
        return view('photo.add', $data);
    }

    public function store(Request $request)
    {
        $type = $request->input('type', '1');
        $fileArray = $request->file('file');
        if (!isset($fileArray)) {
            return back()->with('success','你还没有选择上传图片！');
        }
        foreach ($request->file('file') as $file) {
            $name = $file->getClientOriginalName();//得到图片名；
            $ext = $file->getClientOriginalExtension();//得到图片后缀；
            $fileName = md5(uniqid($name));
            $fileName = $fileName . '.' . $ext;//生成新的的文件名
            $url = '/photo/' . date('Ymd');
            $date = date('Ymd');
            $file->move(base_path() . '/public/photo/' . date('Ymd'), $fileName);
            $size = explode(' ', GetImageSize(base_path() . "/public/photo/$date/" . "$fileName")[3]);
            $sizWidth = (int)substr(explode('=', $size[0])[1], 1, strlen(explode('=', $size[0])[1]) - 2);
            $sizeHeight = (int)substr(explode('=', $size[1])[1], 1, strlen(explode('=', $size[0])[1]) - 1);
            if ($sizeHeight > 1000 || $sizWidth > 1800) {
                Image::make("../public/photo/$date/$fileName")
                    ->resize($sizWidth / 2, $sizeHeight / 2)->save(base_path() . "/public/photo/$date/" . "$fileName");
            }
            $fileUrl['url'] = $url . '/' . $fileName;
            $fileUrl['add_time'] = date('Y-m-d H:i:s');
            $fileUrl['type'] = $type;
            $fileUrl['user_id'] = session('userid');
            $dates[] = $fileUrl;
        }
        DB::beginTransaction();
        try {
            $id = DB::table('xf_photo')->insert($dates);
            DB::commit();
            if ($id) {
                $request->session()->forget('photo');
                back()->with('success','添加图片成功');
                return redirect('/photos');
            } else {
                return back()->with('error','添加图片失败');
            }
        } catch (\Exception $e) {
            DB::rollBack();
            Log::info("添加相册失败" . $e->getTraceAsString());
            return back()->with('error','添加图片失败');
        }
    }
}